﻿using System;
using System.Collections.Generic;
using System.Text;
using Sora.Data.Models;
using Sora.HelpSystem.Package.Pages;
using System.Reflection;
using System.IO;
using Sora.Data;

namespace Sora.HelpSystem.Package
{
    public class HelpDocument : DocumentObject
    {
        static HelpDocument()
        {
            Data.DataFormatProviders.DataFormatsManager.SystemManager.RegisterTypeProvider(typeof(Uri), new DataFormatters.UriDataFormatProvider());
        }

        public static string HelpDocumentUriContentPrefix
        {
            get { return "ds-help://"; }
        }

        #region General Document Architecture

        public HelpDocument() : this(LoadDefaultHelpDocument(), false, true)
        {
        }

        private static Stream LoadDefaultHelpDocument()
        {
            var f_name = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "default.dshelp");

            if (!File.Exists(f_name))
            {
                return new MemoryStream();
            }
            else
            {
                MemoryStream ms = new MemoryStream();

                using (var fs = new FileStream(f_name, FileMode.Open, FileAccess.Read))
                {
                    MTBufferUtils.CopyStreams(fs, ms, fs.Length);
                    ms.Position = 0;
                }

                return ms;
            }
        }

        public HelpDocument(Stream stream, bool readOnly, bool readSource)
            :base(stream, readOnly)
        {
            if (readSource)
                ReadSource();
        }

        public HelpDocument(string fileName, bool readOnly, bool readSource)
            : base(fileName, readOnly)
        {
            if (readSource)
                ReadSource();
        }

        public void ReadSource()
        {
            Database.ReadSourceComplete();
        }

        protected override Guid DatabaseTypeIdentifier
        {
            get { return new Guid("{59D4A02E-44B7-4D7C-B901-0BB8BED1D7A0}"); }
        }

        protected override string DatabaseTypeIdentifierName
        {
            get { return "DreamShield Sora Help File"; }
        }

        #endregion

        public HelpPagesRepository HelpContent { get; private set; }

        internal IEntry InternalContent { get; set; }

        protected override void DocumentInitialize()
        {
            base.DocumentInitialize();

            Database.EnsureSafeState = true;

            HelpContent = new HelpPagesRepository(this, ContentKey.Child("content"));

            InternalContent = ContentKey.Child("internal");
        }

        public Uri StartPageUri
        {
            get { return Get<Uri>("StartPageUri"); }
            set { Set("StartPageUri", value); }
        }

        public Uri DocumentBaseUri
        {
            get { return Get<Uri>("DocumentBaseUri"); }
            set { Set("DocumentBaseUri", value); }
        }

        public string Title
        {
            get { return Get<string>("Title"); }
            set { Set("Title", value); }
        }

        public string Author
        {
            get { return Get<string>("Author"); }
            set { Set("Author", value); }
        }

        public string Company
        {
            get { return Get<string>("Company"); }
            set { Set("Company", value); }
        }

        public Uri Website
        {
            get { return Get<Uri>("Website"); }
            set { Set("Website", value); }
        }
    }
}
